package com.agile.major.service.impl;

import com.agile.major.domain.MajorTraning;
import com.agile.major.domain.TraningData;
import com.agile.major.domain.TraningDataHistory;
import com.agile.major.repository.TraningDataRepository;
import com.agile.major.service.MajorTraningService;
import com.agile.major.service.TraningDataHistoryService;
import com.agile.major.service.TraningDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;


/**
 * Service Implementation for managing TraningData.
 */
@Service
@Transactional
public class TraningDataServiceImpl implements TraningDataService{

    private final Logger log = LoggerFactory.getLogger(TraningDataServiceImpl.class);

    private final TraningDataRepository traningDataRepository;

    private final MajorTraningService majorTraningService;

    private final TraningDataHistoryService traningDataHistoryService;

    public TraningDataServiceImpl(
        TraningDataRepository traningDataRepository,
        MajorTraningService majorTraningService,
        TraningDataHistoryService traningDataHistoryService
    ) {
        this.traningDataRepository = traningDataRepository;
        this.majorTraningService=majorTraningService;
        this.traningDataHistoryService=traningDataHistoryService;

    }

    /**
     * Save a traningData.
     *
     * @param traningData the entity to save
     * @return the persisted entity
     */
    @Override
    public TraningData save(TraningData traningData) {
        log.debug("Request to save TraningData : {}", traningData);
        MajorTraning traning=traningData.getTraning();
        TraningData exist=traningDataRepository.findTraningDataByTraning(traning);
        if(exist!=null){ //如果版本已经保存过，覆盖原数据
            TraningDataHistory history=new TraningDataHistory();
            history.setSaveTime(ZonedDateTime.now());
            history.setTraning(traning);
            history.setTraningJson(exist.getTraningJson());
            traningDataHistoryService.save(history);//保存历史记录

            exist.setTraningJson(traningData.getTraningJson());
            return traningDataRepository.save(exist);
        }else{//如果版本未保存过
            TraningDataHistory history=new TraningDataHistory();
            history.setSaveTime(ZonedDateTime.now());
            history.setTraning(traningData.getTraning());
            history.setTraningJson(traningData.getTraningJson());
            traningDataHistoryService.save(history);//保存历史记录

            return traningDataRepository.save(traningData);
        }

    }

    /**
     *  Get all the traningData.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TraningData> findAll(Pageable pageable) {
        log.debug("Request to get all TraningData");
        return traningDataRepository.findAll(pageable);
    }

    /**
     *  Get one traningData by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public TraningData findOne(Long id) {
        log.debug("Request to get TraningData : {}", id);
        return traningDataRepository.findOne(id);
    }

    /**
     *  Delete the  traningData by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TraningData : {}", id);
        traningDataRepository.delete(id);
    }

    @Override
    public TraningData findByTraning(Long traningId) throws Exception {
        MajorTraning traning=majorTraningService.findOne(traningId);
        if(traning==null){
            throw new Exception("没有找到编号为："+traningId+" 的版本");
        }
        TraningData traningData=traningDataRepository.findTraningDataByTraning(traning);
        return traningData;
    }
}
